package com.example.goods_manage.service.impl;

import com.example.goods_manage.dto.Goods;
import com.example.goods_manage.dto.request.GoodsRequest;
import com.example.goods_manage.mapper.GoodsMapper;
import com.example.goods_manage.service.GoodsService;
import com.example.goods_manage.utils.AjaxResult;
import com.example.goods_manage.utils.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.val;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;

/**
 * <P> @Author: SongKeCheng
 * <P> Type: Class
 * <P> Description:
 * <P> Date: 2024/7/18:15:19
 */

@SuppressWarnings({"unchecked", ""})
@Service
public class GoodsServiceImpl implements GoodsService {
    private static final Logger log = LoggerFactory.getLogger(GoodsServiceImpl.class);

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public AjaxResult findGoods(GoodsRequest goodsRequest) {
        PageHelper.startPage(goodsRequest.getPageNum(), goodsRequest.getPageSize());
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsRequest,goods);
        List<Goods> goodsList =  goodsMapper.findGoods(goods);
        PageInfo<Goods> info = PageInfo.of(goodsList);
        PageUtils pageUtils = new PageUtils(info);
        return AjaxResult.success(pageUtils);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult addGoods(GoodsRequest goodsRequest) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsRequest,goods);

        int result = goodsMapper.insertSelective(goods);
        if(result > 0){
            return AjaxResult.success("新增成功");
        }else {
            return AjaxResult.success("新增失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult updateGoods(GoodsRequest goodsRequest) {
        if (ObjectUtils.isEmpty(goodsRequest.getId())){
        //    id 为空，应该抛出异常 由全局异常处理器来处理
        }
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsRequest,goods);
        int result = goodsMapper.updateByPrimaryKeySelective(goods);
        if(result > 0){
            return AjaxResult.success("更新成功");
        }else {
            return AjaxResult.success("更新失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult deleteGoods(GoodsRequest goodsRequest) {
        int result = goodsMapper.deleteByPrimaryKey(goodsRequest.getId());
        if(result > 0){
            return AjaxResult.success("删除成功");
        }else {
            return AjaxResult.success("删除失败");
        }
    }

    @Value("${goods.retrieval-goods-name-result:20}")
    private Integer retrievalGoodsNameResult;

    @Override
    public List<Map> retrievalGoodsName(String goodsName) {
        PageHelper.startPage(1,retrievalGoodsNameResult);
        List<Map> stringList =  goodsMapper.retrievalGoodsName(goodsName);
        return stringList;
    }
}
